package com.situ.web.dao.impl;

import com.situ.web.dao.IBanjiDao;
import com.situ.web.pojo.entity.Banji;
import com.situ.web.pojo.query.BanjiQuery;
import com.situ.web.pojo.vo.BanjiCountVO;
import com.situ.web.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BanjiDaoImpl implements IBanjiDao {
    @Override
    public List<Banji> selectAll() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Banji> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name from banji";
            statement = connection.prepareStatement(sql);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                Banji banji = new Banji(id, name);
                list.add(banji);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public List<BanjiCountVO> selectBanjiCount() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<BanjiCountVO> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT b.`name`,COUNT(*) AS value\n" +
                    "FROM student as s INNER JOIN banji as b\n" +
                    "ON s.banji_id=b.id\n" +
                    "GROUP BY b.id";
            statement = connection.prepareStatement(sql);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                String name = resultSet.getString("name");
                int value = resultSet.getInt("value");
                BanjiCountVO banjiCountVO = new BanjiCountVO();
                banjiCountVO.setName(name);
                banjiCountVO.setValue(value);
                list.add(banjiCountVO);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public List<Banji> selectByPage(int offset, Integer limit) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Banji> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,address,time from banji limit ?,?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, offset);
            statement.setInt(2, limit);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String address = resultSet.getString("address");
                String time = resultSet.getString("time");
                Banji banji = new Banji(id, name, address, time);
                list.add(banji);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public List<Banji> selectByPage(BanjiQuery banjiQuery) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Banji> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            //  String sql = "select id,name,address,time from banji where name like ? and address like ? and time like ? limit ?,?";
            //  String sql = "select id,name,address,time from banji where name like ? and time like ? limit ?,?";
            //  String sql = "select id,name,address,time from banji where address like ? and time like ? limit ?,?";
            //  String sql = "select id,name,address,time from banji where time like ? limit ?,?";
            //  String sql = "select id,name,address,time from banji where name like ?  limit ?,?";
            //  思路：这个条件不是空，我就拼接上这个条件 and xxx like ?
            //  String sql = "select id,name,address,time from banji where 1=1 ";
            //  if (name is not null) {
            //      sql += " and name like ?"
            //  }
            //  if (address is not null) {
            //      sql += " and address like ?"
            //  }
            //  if (time is not null) {
            //      sql += " and time like ?"
            //  }
            // sql += " limit ?,?";
            String sql = "select id,name,address,time from banji where 1=1";
            //构造一个集合存放所有的条件
            List<Object> queryList = new ArrayList<>();
            String queryName = banjiQuery.getName();
            if (queryName != null && !queryName.equals("")) {
                sql += " and name like ?";
                queryList.add("%" + queryName + "%");
            }
            String queryAddress = banjiQuery.getAddress();
            if (queryAddress != null && !queryAddress.equals("")) {
                sql += " and address like ?";
                queryList.add("%" + queryAddress + "%");
            }
            String queryTime = banjiQuery.getTime();
            if (queryTime != null && !queryTime.equals("")) {
                sql += " and time like ?";
                queryList.add("%" + queryTime + "%");
            }
            sql += " limit ?,?";
            statement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                statement.setObject(i + 1, queryList.get(i));
            }
            int offset = (banjiQuery.getPage() - 1) * banjiQuery.getLimit();
            statement.setInt(queryList.size() + 1, offset);
            statement.setInt(queryList.size() + 2, banjiQuery.getLimit());
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String address = resultSet.getString("address");
                String time = resultSet.getString("time");
                Banji banji = new Banji(id, name, address, time);
                list.add(banji);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public int selectTotalCount() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select count(*) from banji";
            statement = connection.prepareStatement(sql);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        };

        return count;
    }

    @Override
    public int selectTotalCount(BanjiQuery banjiQuery) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select count(*) from banji where 1=1";
            //构造一个集合存放所有的条件
            List<Object> queryList = new ArrayList<>();
            String queryName = banjiQuery.getName();
            if (queryName != null && !queryName.equals("")) {
                sql += " and name like ?";
                queryList.add("%" + queryName + "%");
            }
            String queryAddress = banjiQuery.getAddress();
            if (queryAddress != null && !queryAddress.equals("")) {
                sql += " and address like ?";
                queryList.add("%" + queryAddress + "%");
            }
            String queryTime = banjiQuery.getTime();
            if (queryTime != null && !queryTime.equals("")) {
                sql += " and time like ?";
                queryList.add("%" + queryTime + "%");
            }
            statement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                statement.setObject(i + 1, queryList.get(i));
            }
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        };

        return count;
    }

    @Override
    public void deleteById(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from banji where id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    @Override
    public void add(Banji banji) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into banji(name,address,time) values(?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, banji.getName());
            statement.setString(2, banji.getAddress());
            statement.setString(3, banji.getTime());
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    @Override
    public Banji selectById(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Banji banji = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,address,time from banji where id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果没有返回false，如果有返回true，并且指向这一行
                String name = resultSet.getString("name");
                String address = resultSet.getString("address");
                String time = resultSet.getString("time");
                banji = new Banji(id, name, address, time);
            }
        }  catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
        return banji;
    }

    @Override
    public void update(Banji banji) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update banji set name=?,address=?,time=? where id=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, banji.getName());
            statement.setString(2, banji.getAddress());
            statement.setString(3, banji.getTime());
            statement.setInt(4, banji.getId());
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }
}
